﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RIS.TMP.WEB.APP.Models;
using RIS.TMP.WEB.APP.AddMigrations;

namespace RIS.TMP.WEB.APP.Controllers
{
    public class CFEmployeeController : Controller
    {
        private AllContext db = new AllContext();

        //
        // GET: /CFEmployee/

        public ActionResult Index()
        {
            var cfemployees = db.CFEmployees.Include(c => c.CDivision).Include(c => c.CFBranch).Include(c => c.CFGroupPM).Where(x => x.RecordStatus == "U");
            return View(cfemployees.ToList());
        }

        //
        // GET: /CFEmployee/Details/5

        public ActionResult Details(int id = 0)
        {
            CFEmployee cfemployee = db.CFEmployees.Find(id);
            if (cfemployee == null)
            {
                return HttpNotFound();
            }
            return View(cfemployee);
        }

        //
        // GET: /CFEmployee/Create

        public ActionResult Create()
        {
            ViewBag.DsId = new SelectList(db.CDivisions, "DsId", "DsName");
            ViewBag.BrId = new SelectList(db.CFBranchs, "BrId", "BrName");
            ViewBag.GPMId = new SelectList(db.CFGroupPMs, "GPMId", "GPMName");
            return View();
        }

        //
        // POST: /CFEmployee/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CFEmployee cfemployee)
        {
            if (ModelState.IsValid)
            {
                db.CFEmployees.Add(cfemployee);
                cfemployee.RecordStatus = "U";
                cfemployee.CreateBy = Session["LogedId"].ToString();
                cfemployee.CreateWhen = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DsId = new SelectList(db.CDivisions, "DsId", "DsName", cfemployee.DsId);
            ViewBag.BrId = new SelectList(db.CFBranchs, "BrId", "BrName", cfemployee.BrId);
            ViewBag.GPMId = new SelectList(db.CFGroupPMs, "GPMId", "GPMName", cfemployee.GPMId);
            return View(cfemployee);
        }

        //
        // GET: /CFEmployee/Edit/5

        public ActionResult Edit(int id = 0)
        {
            CFEmployee cfemployee = db.CFEmployees.Find(id);
            if (cfemployee == null)
            {
                return HttpNotFound();
            }
            ViewBag.DsId = new SelectList(db.CDivisions, "DsId", "DsName", cfemployee.DsId);
            ViewBag.BrId = new SelectList(db.CFBranchs, "BrId", "BrName", cfemployee.BrId);
            ViewBag.GPMId = new SelectList(db.CFGroupPMs, "GPMId", "GPMName", cfemployee.GPMId);
            return View(cfemployee);
        }

        //
        // POST: /CFEmployee/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CFEmployee cfemployee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(cfemployee).State = EntityState.Modified;
                cfemployee.UpdateBy = Session["LogedId"].ToString();
                cfemployee.UpdateWhen = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DsId = new SelectList(db.CDivisions, "DsId", "DsName", cfemployee.DsId);
            ViewBag.BrId = new SelectList(db.CFBranchs, "BrId", "BrName", cfemployee.BrId);
            ViewBag.GPMId = new SelectList(db.CFGroupPMs, "GPMId", "GPMName", cfemployee.GPMId);
            return View(cfemployee);
        }

        //
        // GET: /CFEmployee/Delete/5

        public ActionResult Delete(int id = 0)
        {
            CFEmployee cfemployee = db.CFEmployees.Find(id);
            if (cfemployee == null)
            {
                return HttpNotFound();
            }
            return View(cfemployee);
        }

        //
        // POST: /CFEmployee/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CFEmployee cfemployee = db.CFEmployees.Find(id);
            db.Entry(cfemployee).State = EntityState.Modified;
            cfemployee.RecordStatus = "D";
            cfemployee.UpdateBy = Session["LogedId"].ToString();
            cfemployee.UpdateWhen = DateTime.Now;
            //db.CFEmployees.Remove(cfemployee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}